Shader "geometry/grassShader"
{
    Properties
    {
        _DiffTex ("DiffTex", 2D) = "white" {}
        _AlphaTex ("AlphaTex", 2D) = "white" {}

        _Height("Grass Height", float) = 3
		_Width("Grass Width", range(0, 0.1)) = 0.05

    }
    SubShader
    {
        Tags{ "Queue" = "AlphaTest" "RenderType" = "TransparentCutout" "IgnoreProjector" = "True" }

        Pass
        {
            Cull OFF
            Tags{ "LightMode" = "ForwardBase" }
            AlphaToMask On

            CGPROGRAM
            #pragma vertex vert
            #pragma geometry geom
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct v2g
            {
                float4 vertex: SV_POSITION;
                float2 uv : TEXCOORD0;
                float3 normal : NORMAL;
            };

            struct g2f
            {
                float4 vertex : SV_POSITION;
                float3 normal : NORMAL;
                float2 uv : TEXCOORD0;
            };

            sampler2D _DiffTex;
            float4 _MainTex_ST;
            sampler2D _AlphaTex;
            float4 _AlphaTex_ST;

            float _Height;//草的高度
			float _Width;//草的宽度

            v2g vert (appdata_full v)
            {
                v2g o;
                o.vertex = v.vertex;
                o.uv = v.texcoord;
                o.normal = v.normal;
                return o;
            }

            [maxvertexcount(30)]
            void geom(triangle v2g IN[3], inout TriangleStream<g2f> triStream)
            {

                const int vertexNum = 12;

                float4 root = IN[0].vertex;

                float random = sin(UNITY_HALF_PI * (frac(root.x) + frac(root.z)));
                float random2 = sin(UNITY_PI * (root.x + root.z));

                _Width = _Width + (random/50);
                _Height = _Height + (random/5);

                float curHeight = 0;
                float heightOffset = (_Height/(vertexNum/2-1));

                float curV = 0;
                float vOffset = (1.f/(vertexNum/2 -1));

                g2f o[vertexNum] ;

                for(int i=0; i<vertexNum; i++){

                    o[i].normal = float3(0,0,1);

                    if(fmod(i, 2)==0){
                        o[i].vertex = float4(root.x - _Width ,root.y + curHeight, root.z, 1);
                        o[i].uv = float2(0, curV);
                    }
                    else{
                        o[i].vertex = float4(root.x + _Width ,root.y + curHeight, root.z, 1);
                        o[i].uv = float2(1, curV);

                        curHeight += heightOffset;
                        curV += vOffset;
                    }

                    float diffY = o[i].vertex.y - root.y;
                    float offsetX =  random * (diffY*diffY/6) ;
                    float offsetZ = random2 * (diffY*diffY/6) ;

                    float phase = sin(UNITY_HALF_PI *_Time.x * 10);

                    o[i].vertex.x += phase * offsetX;
                    o[i].vertex.z += phase * offsetZ;

                    o[i].vertex = UnityObjectToClipPos(o[i].vertex);
                }

                // 组装三角形
                for(int i = 0;i < 10; i++)
                {
                    triStream.Append(o[i]);
                    triStream.Append(o[i+1]);
                    triStream.Append(o[i+2]);
                }
                triStream.RestartStrip();
            }

            fixed4 frag (g2f i) : SV_Target
            {
                fixed3 col =  tex2D(_DiffTex, i.uv);
                fixed alpha =  tex2D(_AlphaTex, i.uv).g;
                return fixed4(col, alpha);
            }
            ENDCG
        }
    }
}
